package com.wisdom.benefit.jufa.news;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.event.CallBackOtherInterface;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class OrderDetailsAdapter extends BaseAdapter {
    private ArrayList<QueryUserOrderPayListBean> datas;
    private Context context;
    private CallBackOtherInterface callback; //回调接口


    public OrderDetailsAdapter(ArrayList<QueryUserOrderPayListBean> datas, Context context, CallBackOtherInterface callback) {
        this.datas = datas;
        this.context = context;
        this.callback = callback;
    }


    @Override
    public int getCount() {
        return datas.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderDetailsAdapter.ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.itemm_order_details, parent, false);
            holder = new OrderDetailsAdapter.ViewHolder();
            holder.orderId = (TextView) convertView.findViewById(R.id.order_details_orderId);
            holder.orderMoney = (TextView) convertView.findViewById(R.id.order_details_orderMoney);
            holder.orderNo = (TextView) convertView.findViewById(R.id.order_details_orderNo);
            holder.payeeTurn = (TextView) convertView.findViewById(R.id.order_details_payeeTurn);
            holder.payType = (TextView) convertView.findViewById(R.id.order_details_payType);
            holder.status = (TextView) convertView.findViewById(R.id.order_details_status);
            holder.time = convertView.findViewById(R.id.order_details_times_tv);
            holder.btn = (Button) convertView.findViewById(R.id.order_details_btn);
            holder.btn1 = (Button) convertView.findViewById(R.id.order_details_btn1);
            convertView.setTag(holder);
        } else {
            holder = (OrderDetailsAdapter.ViewHolder) convertView.getTag();
        }

        holder.payeeTurn.setText(String.valueOf(datas.get(position).getPayeeTurn()));
        holder.orderMoney.setText(String.valueOf(datas.get(position).getOrderMoney()));
        if (datas.get(position).getPayType() == 1) {
            holder.payType.setText("支付宝固码");
        } else if (datas.get(position).getPayType() == 2) {
            holder.payType.setText("微信固码");
        } else if (datas.get(position).getPayType() == 3) {
            holder.payType.setText("支付宝转账");
        }
        SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formats.format(new java.util.Date());

//时间格式化

        Date dateCurrent = null;
        try {
            dateCurrent = formats.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

//dataMake为要比较的时间

        Date selectTime = null;
        try {
            selectTime = formats.parse(datas.get(position).getGmtCreate());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //gethours为时间差
        Long gethours = (dateCurrent.getTime() - selectTime.getTime()) / (3600 * 1000);
        holder.time.setText(datas.get(position).getGmtCreate());
        holder.orderNo.setText(datas.get(position).getOrderNo());
        holder.orderId.setText(String.valueOf(datas.get(position).getOrderId()));
        if (datas.get(position).getStatus() == 1) {
            if (gethours >= 1) {
                holder.status.setText("未付款");
                holder.btn1.setEnabled(false);
                holder.btn.setEnabled(false);
            } else {
                holder.status.setText("未付款");
                holder.btn1.setEnabled(true);
                holder.btn.setEnabled(false);
            }

        } else if (datas.get(position).getStatus() == 2) {
            holder.status.setText("已付款");
            holder.btn1.setEnabled(false);
            if (datas.get(position).getReceiptStatus() == 0) {
                holder.btn.setEnabled(false);
                holder.btn.setText("审核中");
            } else if (datas.get(position).getReceiptStatus() == 1) {
                holder.btn.setEnabled(false);
                holder.btn.setText("审核成功");
            } else {
                holder.btn.setEnabled(true);
                holder.btn.setText("回单");
            }

        } else if (datas.get(position).getStatus() == 3) {
            holder.status.setText("申诉中");
            holder.btn1.setEnabled(false);
            holder.btn.setEnabled(false);
        } else if (datas.get(position).getStatus() == 4) {
            holder.status.setText("已失效");
            holder.btn1.setEnabled(false);
            holder.btn.setEnabled(false);
        } else if (datas.get(position).getStatus() == 5) {
            holder.status.setText("已完成");
            holder.btn1.setEnabled(false);
            holder.btn.setEnabled(false);
        }
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.callBackClick(position);
            }
        });
        holder.btn.setTag(position);
        holder.btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.callBackClickOther(position);
            }
        });
        holder.btn1.setTag(position);
        return convertView;
    }

    static class ViewHolder {
        TextView orderId, orderNo, payType, orderMoney, payeeTurn, status, time;
        Button btn, btn1;
    }

    //布局改变时用来刷新ListView
    public void onDateChanged(ArrayList<QueryUserOrderPayListBean> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }


}

